package figure8.GUI;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JToggleButton;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import figure8.Controller;
import figure8.DrumMachineTrack;
import figure8.MIDITrack;
import figure8.Track;
import figure8.WAVTrack;
import figure8.events.TrackChangeListener;
import figure8.events.TrackChangedEvent;

public class DrumMachineSettingsPanel extends JPanel implements TrackChangeListener {
	private final Controller controller;
	private final DrumMachineTrack track;
	private File[] drums;
	DrumMachineGraph drumGraph;
	
	private JToggleButton mute;
	private JSlider volume;
	
	public DrumMachineSettingsPanel(final Controller controller, final DrumMachineTrack track, DrumMachineGraph graph) {
		super();
		drumGraph = graph;
		//I know this is super ugly at the moment but I just want to get the instrument selector working first! 
		GridLayout gridy = new GridLayout (0, 1);
		GridLayout grid = new GridLayout (0, 2);
		this.setLayout(gridy);
		this.setPreferredSize(new Dimension(230, 150));
		this.controller = controller;
		this.track = track;
		this.add(new JLabel("Drum Machine"));
		JPanel mutingPanel = new JPanel();
		mutingPanel.setLayout(grid);
		JPanel volumePanel = new JPanel();
		volumePanel.setLayout(grid);
		//mutingPanel.setLayout(new BoxLayout(mutingPanel));
		
		final JToggleButton mute = new JToggleButton("M");
		this.mute = mute;
		mutingPanel.add(mute);
		
		final JButton sounds = new JButton("Sounds");
		mutingPanel.add(sounds);
		
		sounds.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				new DrumMachineSoundsWindow(controller, track);
			}
		});
		
		final JButton editLoop = new JButton("Edit");
		
		final DrumMachineSettingsPanel settingsPanel = this;
		editLoop.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				DrumMachineEditWindow editWindow = new DrumMachineEditWindow(controller, track.samples(), track, settingsPanel);
			}
		});
		
		
		mute.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				track.setMuted(!track.getMuted());
				track.fireTrackChangedEvent(true);
			}
		});
		
		//JPanel volumePanel = new JPanel();
		//volumePanel.setLayout(new BoxLayout(volumePanel, BoxLayout.Y_AXIS));
		
		final JSlider volume = new JSlider(-30, 14, (int) track.getVolume());
		this.volume = volume;
		volumePanel.add(volume);
		
		//this.add(volumePanel);
		volumePanel.add(editLoop);
		this.add(mutingPanel);
		//this.add(new JLabel("Volume"));
		this.add(volumePanel);
		
		volume.addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent arg0) {
				if (track.getVolume() != volume.getValue()) {	
					track.setVolume(volume.getValue());
					track.fireTrackChangedEvent(true);
				}
			}
		});
		
		track.addChangeListener(this);
	}
	
	public void changeGraph() {
		drumGraph.changeGraph();
	}

	@Override
	public void trackChanged(TrackChangedEvent event) {
		this.mute.setSelected(this.track.getMuted());
		this.volume.setValue((int) this.track.getVolume());
	}
}
